Process.Start 方法 (System.Diagnostics) 您所在的位置:网站首页 series start process error翻译 Process.Start 方法 (System.Diagnostics)

Process.Start 方法 (System.Diagnostics)

2024-07-11 07:42| 来源: 网络整理| 查看: 265

Source:Process.cs Source:Process.cs Source:Process.cs

启动由包含进程启动信息(例如,要启动的进程的文件名)的参数指定的进程资源,并将该资源与新的 Process 组件关联。

public: static System::Diagnostics::Process ^ Start(System::Diagnostics::ProcessStartInfo ^ startInfo); public static System.Diagnostics.Process? Start (System.Diagnostics.ProcessStartInfo startInfo); [System.Runtime.Versioning.UnsupportedOSPlatform("ios")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] public static System.Diagnostics.Process? Start (System.Diagnostics.ProcessStartInfo startInfo); [System.Runtime.Versioning.UnsupportedOSPlatform("ios")] [System.Runtime.Versioning.UnsupportedOSPlatform("tvos")] [System.Runtime.Versioning.SupportedOSPlatform("maccatalyst")] public static System.Diagnostics.Process? Start (System.Diagnostics.ProcessStartInfo startInfo); public static System.Diagnostics.Process Start (System.Diagnostics.ProcessStartInfo startInfo); static member Start : System.Diagnostics.ProcessStartInfo -> System.Diagnostics.Process [] [] static member Start : System.Diagnostics.ProcessStartInfo -> System.Diagnostics.Process [] [] [] static member Start : System.Diagnostics.ProcessStartInfo -> System.Diagnostics.Process Public Shared Function Start (startInfo As ProcessStartInfo) As Process 参数 startInfo ProcessStartInfo

ProcessStartInfo,包含用于启动进程的信息(包括文件名和任何命令行参数)。

返回 Process

与进程资源关联的新 Process,如果未启动进程资源,则为 null。 请注意,伴随同一进程中已运行的实例而启动的新进程将独立于其他进程。 此外,启动可能返回一个 HasExited 属性已设置为 true 的非 null 进程。 在这种情况下,启动的进程可能已激活现有实例自身,然后退出。

属性 UnsupportedOSPlatformAttribute SupportedOSPlatformAttribute 例外 InvalidOperationException

在 startInfo 参数的 FileName 属性中未指定任何文件名。

startInfo 参数的 UseShellExecute 属性值为 true,且 RedirectStandardInput、RedirectStandardOutput 或 RedirectStandardError 属性的值也为 true。

startInfo 参数的 UseShellExecute 属性值为 true,且 UserName 属性值不为 null 或空,或者 Password 属性值不为 null。

ArgumentNullException

startInfo 参数为 null。

ObjectDisposedException

已释放此进程对象。

Win32Exception

打开关联的文件时出错。

在 startInfo 参数的 FileName 属性中指定的文件未找到。

参数的长度与该进程的完整路径的长度的总和超过了 2080。 与此异常关联的错误消息可以是以下情况之一:“传递给系统调用的数据区域太小”。或“拒绝访问”。

PlatformNotSupportedException

不支持 shell 的操作系统(如,仅适用于.NET Core 的 Nano Server)不支持此方法。

示例

以下示例首先生成 Internet Explorer 的实例,并在浏览器中显示“收藏夹”文件夹的内容。 然后,它会启动 Internet Explorer 的一些其他实例,并显示一些特定的页面或站点。 最后,它会启动 Internet Explorer,在导航到特定站点时最小化窗口。

有关此方法的其他用法的其他示例,请参阅 类的各个 ProcessStartInfo 属性。

#using using namespace System; using namespace System::Diagnostics; using namespace System::ComponentModel; // Opens the Internet Explorer application. void OpenApplication(String^ myFavoritesPath) { // Start Internet Explorer. Defaults to the home page. Process::Start("IExplore.exe"); // Display the contents of the favorites folder in the browser. Process::Start(myFavoritesPath); } // Opens urls and .html documents using Internet Explorer. void OpenWithArguments() { // URLs are not considered documents. They can only be opened // by passing them as arguments. Process::Start("IExplore.exe", "www.northwindtraders.com"); // Start a Web page using a browser associated with .html and .asp files. Process::Start("IExplore.exe", "C:\\myPath\\myFile.htm"); Process::Start("IExplore.exe", "C:\\myPath\\myFile.asp"); } // Uses the ProcessStartInfo class to start new processes, // both in a minimized mode. void OpenWithStartInfo() { ProcessStartInfo^ startInfo = gcnew ProcessStartInfo("IExplore.exe"); startInfo->WindowStyle = ProcessWindowStyle::Minimized; Process::Start(startInfo); startInfo->Arguments = "www.northwindtraders.com"; Process::Start(startInfo); } int main() { // Get the path that stores favorite links. String^ myFavoritesPath = Environment::GetFolderPath(Environment::SpecialFolder::Favorites); OpenApplication(myFavoritesPath); OpenWithArguments(); OpenWithStartInfo(); } using System; using System.Diagnostics; using System.ComponentModel; namespace MyProcessSample { class MyProcess { // Opens the Internet Explorer application. void OpenApplication(string myFavoritesPath) { // Start Internet Explorer. Defaults to the home page. Process.Start("IExplore.exe"); // Display the contents of the favorites folder in the browser. Process.Start(myFavoritesPath); } // Opens urls and .html documents using Internet Explorer. void OpenWithArguments() { // url's are not considered documents. They can only be opened // by passing them as arguments. Process.Start("IExplore.exe", "www.northwindtraders.com"); // Start a Web page using a browser associated with .html and .asp files. Process.Start("IExplore.exe", "C:\\myPath\\myFile.htm"); Process.Start("IExplore.exe", "C:\\myPath\\myFile.asp"); } // Uses the ProcessStartInfo class to start new processes, // both in a minimized mode. void OpenWithStartInfo() { ProcessStartInfo startInfo = new ProcessStartInfo("IExplore.exe"); startInfo.WindowStyle = ProcessWindowStyle.Minimized; Process.Start(startInfo); startInfo.Arguments = "www.northwindtraders.com"; Process.Start(startInfo); } static void Main() { // Get the path that stores favorite links. string myFavoritesPath = Environment.GetFolderPath(Environment.SpecialFolder.Favorites); MyProcess myProcess = new MyProcess(); myProcess.OpenApplication(myFavoritesPath); myProcess.OpenWithArguments(); myProcess.OpenWithStartInfo(); } } } Imports System.Diagnostics Imports System.ComponentModel Namespace MyProcessSample Class MyProcess ' Opens the Internet Explorer application. Public Sub OpenApplication(myFavoritesPath As String) ' Start Internet Explorer. Defaults to the home page. Process.Start("IExplore.exe") ' Display the contents of the favorites folder in the browser. Process.Start(myFavoritesPath) End Sub ' Opens URLs and .html documents using Internet Explorer. Sub OpenWithArguments() ' URLs are not considered documents. They can only be opened ' by passing them as arguments. Process.Start("IExplore.exe", "www.northwindtraders.com") ' Start a Web page using a browser associated with .html and .asp files. Process.Start("IExplore.exe", "C:\myPath\myFile.htm") Process.Start("IExplore.exe", "C:\myPath\myFile.asp") End Sub ' Uses the ProcessStartInfo class to start new processes, ' both in a minimized mode. Sub OpenWithStartInfo() Dim startInfo As New ProcessStartInfo("IExplore.exe") startInfo.WindowStyle = ProcessWindowStyle.Minimized Process.Start(startInfo) startInfo.Arguments = "www.northwindtraders.com" Process.Start(startInfo) End Sub Shared Sub Main() ' Get the path that stores favorite links. Dim myFavoritesPath As String = Environment.GetFolderPath(Environment.SpecialFolder.Favorites) Dim myProcess As New MyProcess() myProcess.OpenApplication(myFavoritesPath) myProcess.OpenWithArguments() myProcess.OpenWithStartInfo() End Sub End Class End Namespace 'MyProcessSample 注解

使用此重载通过指定 ProcessStartInfo 实例来启动进程资源。 重载将资源与新 Process 对象关联。

重要

使用不受信任的数据调用此方法存在安全风险。 仅使用受信任的数据调用此方法。 有关详细信息,请参阅 验证所有输入。

注意

如果要启动的可执行文件的地址为 URL,则进程不会启动,并且 null 返回。

此重载使你无需先创建新 Process 实例即可启动进程。 将此重载与参数一 ProcessStartInfo 起使用是创建新 Process 实例、设置其 StartInfo 属性和 Start 调用实例的显式步骤的 Process 替代方法。

ProcessStartInfo使用 实例作为 参数,可以Start调用时对传入调用的内容进行最多控制,以启动进程。 如果只需要传递文件名或文件名和参数,则无需创建新 ProcessStartInfo 实例,尽管这是一个选项。 唯 Process.StartInfo 一必须设置的属性是 FileName 属性。 属性 FileName 不需要表示可执行文件。 它可以是扩展已关联到系统上安装的应用程序的任何文件类型。 例如,FileName如果已将文本文件与编辑器(如记事本)关联,则 属性可以具有 .txt 扩展名;如果已将 .doc 文件与文字处理工具(如 Microsoft Word)相关联,则属性可以具有 .doc 扩展名。

可以通过指定位置 (启动 ClickOnce 应用程序,例如,最初安装应用程序的 Web 地址) 。 不要通过指定 ClickOnce 应用程序在硬盘驱动器上的安装位置来启动该应用程序。

ProcessStartInfo.UserName如果设置了 实例的 StartInfo 和 ProcessStartInfo.Password 属性,则会调用非托管CreateProcessWithLogonW函数,即使属性值为 true 或 ProcessStartInfo.WindowStyle 属性值为 ProcessWindowStyle.Hidden,该函数也会在新窗口中ProcessStartInfo.CreateNoWindow启动进程。 ProcessStartInfo.Domain如果 属性为 null,则ProcessStartInfo.UserName属性必须采用 UPN 格式,用户@DNS_domain_name。

与其他重载不同,没有参数的 重 Start 载不是成员 static 。 如果已创建 Process 实例并指定启动信息 (包括文件名) ,并且想要启动进程资源并将其与现有 Process 实例关联,请使用该重载。 如果要创建新Process组件,而不是为现有组件启动进程,请使用其中static一个重载。 此重载和没有参数的重载都允许使用 ProcessStartInfo 实例指定进程资源的启动信息。

如果在系统中使用引号声明了路径变量,则必须在启动在该位置找到的任何进程时完全限定该路径。 否则,系统将找不到路径。 例如,如果 c:\mypath 不在路径中,并且你使用引号添加它: path = %path%;"c:\mypath",则必须在启动它时完全限定中的任何 c:\mypath 进程。

注意

ASP.NET 网页和服务器控制代码在 Web 服务器上 ASP.NET 工作进程的上下文中执行。 如果在 ASP.NET 网页或服务器控件中使用 Start 方法,则新进程将在具有受限权限的 Web 服务器上执行。 进程不会在客户端浏览器所在的上下文中启动,并且无权访问用户桌面。

每当使用 Start 启动进程时,都可能需要关闭它,否则可能会丢失系统资源。 使用 CloseMainWindow 或 Kill关闭进程。 可以使用进程HasExited属性检查进程是否已关闭。

此处需要说明托管线程中的单元状态。 当 位于 true 参数上startInfo时UseShellExecute,请确保通过在 方法上设置 属性[STAThread],在应用程序上main()设置线程模型。 否则,托管线程可能处于 状态 unknown 或处于 MTA 状态,后者与 UseShellExecute 为 true冲突。 某些方法要求单元状态不是 unknown。 如果未显式设置状态,当应用程序遇到此类方法时,它默认为 MTA,并且一旦设置,将无法更改单元状态。 但是, MTA 会导致在操作系统 shell 管理线程时引发异常。

另请参阅 StartInfo FileName ProcessStartInfo CloseMainWindow() Kill() 适用于


【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有